// 作用：修改全局用户信息，并缓存
import { userDetailApi } from '@/api';
import { userStore } from '@/stores';
import { isSuccess } from '@/utils';
import { useSetAtom } from 'jotai';
import store from 'storejs';

export default function useUserInfo() {
  const setUserStore = useSetAtom(userStore);

  // 获取用户信息
  const getUserInfo = async () => {
    const userId = store.get('userInfo');
    const res: any = await userDetailApi({ id: +userId?.id });
    if (isSuccess(res)) {
      const val = res.data;
      // 更新store
      setUserStore(val);
      return val;
    } else {
      return false;
    }
  };

  return { getUserInfo };
}
